﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guline.Web.CentralModels;
using Guline.Web.CentralModels.Object;
using Guline.Web.Central.Controllers;
using Guline.PaymentAPI;
using Newtonsoft.Json.Linq;
using System.Data;
using Guline.Web.Central.Party;
using System.Data.Entity;
namespace Guline.Web.Central.Areas.Messages.Controllers
{
    public class MessageController : BaseController
    {
        //
        // GET: /Messages/Message/

        public ActionResult Index()
        {
            return View(db.Messages.Where(m => m.Account.UserName == User.Identity.Name).Include(m=>m.Contact).OrderByDescending(m=>m.ID).ToList());
        }
        public ActionResult CreateSMS()
        {
            ViewBag.ContactID = new SelectList(this.db.Contacts.Where(m => m.Account.UserName == User.Identity.Name).ToList(), "ID", "ContactName");
            return View();
        }
        public ActionResult GetPhoneNumber(int ID)
        {
            string phoneNum = this.db.Contacts.Where(m => m.ID == ID).FirstOrDefault().PhoneNumber;
            return Json(phoneNum, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult CreateSMS(Message model)
        {
            if (ModelState.IsValid)
            {
                model.AccountID = db.Accounts.Where(m => m.UserName == User.Identity.Name).FirstOrDefault().ID;
                model.DateCreated = DateTime.Now;
                var Device = db.Devices.Where(m => m.Account.UserName == User.Identity.Name && m.Enable == true).FirstOrDefault();
                if (Device == null)
                {
                    ModelState.AddModelError("NotDevice", "Create Device!");
                }
                else
                {

                    model.DeviceID = Device.ID;
                    this.db.Messages.Add(model);
                    this.db.SaveChanges();
                    var appfirst = db.CApps.Where(m => m.AccountID == model.AccountID).FirstOrDefault();
                    NotificationService ns = new NotificationService();
                    if (ns.SendSms(appfirst.Accesskey, model.ToPhoneNumber, model.Content, model.ID))
                    {
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ModelState.AddModelError("NotDevice", "Can not send SMS!");
                    }
                }
            }
            ViewBag.ContactID = new SelectList(this.db.Contacts.Where(m => m.Account.UserName == User.Identity.Name).ToList(), "ID", "ContactName");
            return View();
        }
        public ActionResult EidtSMS(int ID)
        {
            var sms = db.Messages.Where(m => m.ID == ID).FirstOrDefault();
            //ViewBag.AccountID = new SelectList(this.db.Accounts, "ID", "UserName", selectedValue: app.AccountID);
            return View(sms);
        }
        [HttpPost]
        public ActionResult EidtSMS(Message model)
        {
            if (ModelState.IsValid)
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();
        }
        public ActionResult DeleteSMS(int ID)
        {
            var sms = db.Messages.Where(m => m.ID == ID).FirstOrDefault();
            db.Messages.Remove(sms);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

    }
}
